using System;
using System.Collections.Generic;
using System.Collections;
using System.Text;
using System.Web.UI.WebControls;
namespace Ent.Web.WebControl
{
    public abstract class UserControl : System.Web.UI.UserControl
    {
        private string _loadEvents = "";
        private string _width = "100%";
        private string _runtimeEvents = "";
        private string _javascript = "";
        private string _lang = "";
        Dictionary<string, object> _oldValues = null;
        public Dictionary<string, object> Values
        {
            get { return _oldValues; }
            set { _oldValues = value; }
        }
        Dictionary<string, string> _params = null;
        public Dictionary<string, string> Parameters
        {
            get { return _params; }
            set { _params = value; }
        }
        protected void LoadJavascript(string path)
        {
            _javascript += Javascript.ReadAllText(Server.MapPath(path), new string[] { _loadEvents }) + "\r\n";
        }
        protected string JavascriptText
        {
            get
            {
                string retValue = "\r\n<script type=\"text/javascript\">\r\n\t" + _javascript + GetRuntimeEvent() + "</script>";
                return retValue;
            }
        }
        protected void AddRuntimeEvent(string name, string[] statements)
        {
            _runtimeEvents += Javascript.CreateFunction(name, statements) + "\r\n";
        }
        private string GetRuntimeEvent()
        {
            string retValue = "";
            retValue += "function EnableDisable(source, effect)";
            retValue += "\r\n{";
            retValue += "\r\n\tif(!document.getElementById(source).checked || document.getElementById(source).disabled)";
            retValue += "\r\n\t\tSetDisabled(document.getElementById(effect), true)";
            retValue += "\r\n\telse";
            retValue += "\r\n\t\tSetDisabled(document.getElementById(effect), false)";
            retValue += "\r\n\treturn true;";
            retValue += "\r\n}";
            retValue += "\r\nfunction SetDisabled(el,val) {";
            retValue += "\r\n\tif(el==null || typeof(el)=='undefined') return;";
            retValue += "\r\n\ttry";
            retValue += "\r\n\t{";
            retValue += "\r\n\t\tel.disabled = val;";
            retValue += "\r\n\t\tif((el.type == 'text' || el.type == 'textarea') && val) el.className='textbox textboxDis';";
            retValue += "\r\n\t\tif((el.type == 'text' || el.type == 'textarea') && !val) el.className='textbox textboxMan';";
            retValue += "\r\n\t}";
            retValue += "\r\n\tcatch(E){}";
            retValue += "\r\n\tif (el.childNodes && el.childNodes.length > 0)";
            retValue += "\r\n\t{";
            retValue += "\r\n\t\tfor (var x = 0; x < el.childNodes.length; x++)";
            retValue += "\r\n\t\t{";
            retValue += "\r\n\t\t\tSetDisabled(el.childNodes[x],val);";
            retValue += "\r\n\t\t}";
            retValue += "\r\n\t}";
            retValue += "\r\n}";

            retValue += _runtimeEvents;
            return retValue;
        }
        protected void SetOnClickEvent(Parameters.OnclickParam param)
        {
            SetOnClickEvent(param, null);
        }
        protected void SetOnClickEvent(Parameters.OnclickParam param, Parameters.OnclickParam[] linkedParams)
        {
            int lDef = 0;
            int lNew=0;
            if (linkedParams != null)
                lNew = linkedParams.Length;
            if (param.LinkedOnclock != null)
                lDef = param.LinkedOnclock.Length;
            Parameters.OnclickParam[] appendedParams = new Parameters.OnclickParam[lDef + lNew];
            if (lDef != 0)
            {
                for (int i = 0; i < lDef; i++)
                {
                    appendedParams.SetValue((Parameters.OnclickParam)param.LinkedOnclock.GetValue(i), i);
                }
            }
            if (lNew != 0)
            {
                for (int i = lDef; i < (lDef + lNew); i++)
                {
                    appendedParams.SetValue((Parameters.OnclickParam)linkedParams.GetValue(i), i);
                }
            }
            Parameters.OnclickParam newParan = new Parameters.OnclickParam(param.Source,
                param.SourceChild,
                param.EffectId,
                param.Functions,
                appendedParams);
            param.Source.Attributes.Add("onclick", GetOnClickEventString(newParan));
        }
        private string GetOnClickEventString(Parameters.OnclickParam param)
        {
            string doEvent = "";
            if (param == null)
                return doEvent;
            if (param.SourceChild == null || param.SourceChild == "")
                param.SourceChild = param.Source.ClientID;
            if (param.EffectId != null && param.EffectId != "")
            {
                doEvent += "EnableDisable('" + param.SourceChild + "', '" + param.EffectId + "');";
            }
            if (param.LinkedOnclock != null)
            {
                for (int i = 0; i < param.LinkedOnclock.Length; i++)
                {
                    doEvent += GetOnClickEventString((Parameters.OnclickParam)param.LinkedOnclock.GetValue(i));
                }
            }
            if (param.Functions != null)
            {
                for (int i = 0; i < param.Functions.Length; i++)
                {
                    doEvent += param.Functions.GetValue(i).ToString() + ";";
                }
            }
            return doEvent;
        }
        /// <summary>
        /// Disables controlId when condition is true.
        /// Also calls the function array.
        /// </summary>
        /// <param name="condition"></param>
        /// <param name="controlId"></param>
        /// <param name="isServerControl"></param>
        /// <param name="functions"></param>
        protected void SetOnLoadEvent(bool condition, string controlId, string[] functions)
        {
            if (condition)
            {
                _loadEvents += "SetDisabled(document.getElementById('" + controlId + "'),true);\r\n";
                if (functions != null)
                {
                    for (int i = 0; i < functions.Length; i++)
                    {
                        _loadEvents += functions.GetValue(i).ToString() + ";";
                    }
                }
            }
        }
        protected void OnLoadDisable(string[] controls)
        {
            if (controls != null)
            {
                for (int i = 0; i < controls.Length; i++)
                {
                    _loadEvents += "SetDisabled(document.getElementById('" + controls.GetValue(i).ToString() + "'),true);\r\n";
                }
            }
        }
        protected void OnLoadDisable(string control)
        {
            if (control != null)
            {
                _loadEvents += "SetDisabled(document.getElementById('" + control + "'),true);\r\n";
            }
        }
        protected void OnLoadEnable(string[] controls)
        {
            if (controls != null)
            {
                for (int i = 0; i < controls.Length; i++)
                {
                    _loadEvents += "SetDisabled(document.getElementById('" + controls.GetValue(i).ToString() + "'),false);\r\n";
                }
            }
        }
        protected void OnLoadEnable(string control)
        {
            if (control != null)
            {
                _loadEvents += "SetDisabled(document.getElementById('" + control + "'),false);\r\n";
            }
        }
        protected void OnLoadExecute(string[] functions)
        {
            if (functions != null)
            {
                for (int i = 0; i < functions.Length; i++)
                {
                    _loadEvents += functions.GetValue(i).ToString() + ";";
                }
            }
        }
        protected void OnLoadExecute(string function)
        {
            if (function != null)
            {
                _loadEvents += function + ";";
            }
        }
        protected void SetCollectionControl(ListControl lc, string[] list)
        {
            for (int i = 0; i < lc.Items.Count; i++)
                lc.Items[i].Selected = false;
            for (int i = 0; i < list.Length; i++)
            {

                ListItem li = lc.Items.FindByValue(list.GetValue(i).ToString());
                if (li != null)
                    li.Selected = true;
            }
        }
        public string Width
        {
            get { return _width; }
            set { _width = value; }
        }
        public string Language
        {
            get {
                if (_lang == "" && Session["LANG"] != null)
                    _lang = Session["LANG"].ToString();
                return _lang; 
            }
            set { _lang = value; }
        }
        private Dictionary<string, object> _aspxParam = new Dictionary<string,object>();
        /// <summary>
        /// Needed to transfers values from aspx to ascx
        /// </summary>
        public object GetASPXParam(string key)
        {
            if (_aspxParam.ContainsKey(key))
                return _aspxParam[key];
            else
                return null;
        }
        public void AddASPXParam(string name, object value)
        {
            if (!_aspxParam.ContainsKey(name))
                _aspxParam.Add(name, value);
            else
                throw new Exception(name + " already present in ASPX Param.");
        }
        public abstract void Init();
        public abstract Dictionary<string, object> GetValues();
        public abstract void Dispose();
        public abstract void SetEvents();
        public abstract void AddJavascript();
    }
}
